package com.xybbs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xybbs.entity.Forums;
import com.xybbs.entity.Users;
import com.xybbs.service.ForumsService;

@RequestMapping("forum")
@Controller
public class ForumController {
	
	@Autowired
	ForumsService forumsService;
	
	@GetMapping("toForumManagePage.do")
	public String toForumManagePage(HttpServletRequest req) {
		//获取session里的user，如果没有则是没有登录，返回错误页面，防止普通用户误入后台页面
		Users user = (Users) req.getSession().getAttribute("user");
		if(user == null) {
			req.setAttribute("msg", "页面不见了");
			return "404";
		}
		List<Forums> list = forumsService.checkAll();
		req.setAttribute("forums", list);
		return "forumManage";
	}

	@GetMapping("disEnableForum.do")
	public String disEnableForum(String forumId) {
		forumsService.disEnableForum(forumId);
		return "redirect:toForumManagePage.do";
	}
	
	@PostMapping("addForum.do")
	public String addForum(Forums forum) {
		forumsService.addForum(forum);
		return "redirect:toForumManagePage.do";
	}

}
